/*
 * Given a string, find the length of the longest substring
 * without repeating characters. For example, the longest
 * substring without repeating letters for "abcabcbb" is "abc",
 * which the length is 3. For "bbbbb" the longest substring is
 * "b", with the length of 1.
 */
#include <stdio.h>
#include <ctype.h>

int lengthOfLongestSubstring(char *s) {
    int index[256];
    int i = 0;
    int len = strlen(s);
    int last_pos = -1, longest = 0;

    memset(index, 0, sizeof(int) * 256);

    for (i = 0; i < len; ++i) {
        last_pos = index[s[i]] > last_pos ? index[s[i]] : last_pos;
        index[s[i]] = i + 1;
        longest = i - last_pos + 1 > longest ? i - last_pos + 1 : longest;
    }

    return longest;
}

int main() {
    char *s = "Hello World!";
    int longets = lengthOfLongestSubstring(s);
    printf("%d\n", longets);

    return 0;
}
